﻿using System;
using System.Threading;
using System.Collections.Generic;
using System.Text;

namespace AliIotAmqpWorker
{
    /// <summary>
    /// 基础任务
    /// </summary>
    public abstract class BaseTaskProvider : IDisposable
    {
        /// <summary>
        /// 任务暂停标识位
        /// </summary>
        public volatile bool PauseBit = false;
        /// <summary>
        /// 任务停止标识位
        /// </summary>
        public volatile bool StopBit = false;
        /// <summary>
        /// 任务加载过程中标识位
        /// </summary>
        public volatile bool IsLoadingBit = false;
        /// <summary>
        /// 线程
        /// </summary>
        public Thread thread { set; get; }
        /// <summary>
        /// 线程id信息
        /// </summary>
        public int TaskId { set; get; }

        public BaseTaskProvider()
        {
            thread = new Thread(TaskCycleHandle);
            thread.Start();
        }

        /// <summary>
        /// 任务周期循环处理
        /// </summary>
        public virtual void TaskCycleHandle()
        {

        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public virtual void Dispose()
        {
            if (thread != null)
            {
                try
                {
                    if (thread.ThreadState == ThreadState.Running)
                    {
                        thread.Abort();
                        thread.Join();
                    }

                    thread = null;
                }
                catch (Exception ex)
                { }
            }
        }
    }
}
